<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/12
 * Time: 17:16
 */

class Size_model extends CI_Model
{

    public $table_name = 'size';

    public $code_setting = [
        0 => '无尺码(35)',
        1 => 'S(36)',
        2 => 'M(37)',
        3 => 'L(38)',
        4 => 'XL(39)',
        5 => '2XL(40)',
        6 => '3XL(41)',
        7 => '4XL(42)',
        8 => '5XL(43)'
    ];
    
    public $limit_rows = 15;
    
    public function size_option()
    {

//        $query = $this->db->from('size')->where("size_type = 2 and size_on <> '00'")->get();
        //暂时放开尺码查询
        $query = $this->db->from('size')->where(['is_delete'=>0])->where(" size_on <> '00'")->get();

        $option = array();
        $size = array();
        foreach ($query->result_array() as $row) {
            $option[$row['size_id']] = $row['size_value'];
            $size[$row['size_value']] = $row;
        }

        return array('option' => $option, 'size' => $size);
    }
    
    public function _where($param = array())
    {
        if ($param['size_name']) {
            $this->db->where("(size_value LIKE '%{$param['size_name']}%' OR size_code LIKE '%{$param['size_name']}%')");
        }
    }
    
    public function size_list($param = array())
    {
        $this->db->from('size')
            ->limit($this->limit_rows,$this->limit_rows * $param['page'])
            ->order_by('size_id desc');
        
        $this->_where($param);
        
        $query = $this->db->get();
        
        $size = $query->result_array();
        
        return $size;
    }
    
    public function count($param = array())
    {
        $this->db->from('size');
        
        $this->_where($param);
        
        return $this->db->count_all_results();
    }
    
    public function size_info($param = array())
    {
        $query = $this->db->from('size')->where($param)->get();
        
        return $query->row_array();
    }
    
    public function insert_size($data = array(), $id = 0)
    {
        $time = time();
        
        $size_data = array(
            'size_value' => $data['size_value'],
            'size_code' => $data['size_code'],
            'size_type' => $data['size_type'],
            'new_size_code' => $data['new_size_code'],
            'is_delete' => $data['is_delete']
        );
        
        if ($id) {
            $this->db->where('size_id', $id);
            $this->db->update('size', $size_data);
        } else {
            $size_data['size_num'] = $this->max_size_num($size_data['size_type']);
            $this->db->insert('size', $size_data);
        }
    }
    
    public function max_size_num($size_type = 0)
    {
        $query = $this->db->from('size')
            ->select_max('size_num')
            ->where('size_type', $size_type)
            ->get();
        
        $size = $query->row_array();
        
        return $size['size_num'] + 1;
    }
}